<?php
/*****************************************
 This file is part of the Dynamo CMS UI Toolkit
 Copyright (c) Dynamo Studios 2008

 *** NOTE ***
 Permission is hereby granted to execute this file as a
 web service under the terms of the license agreement.

 See LICENSE.txt, in the system folder.

 All other rights, other than those expressed in the license
 document, are reserved.

 ****************************************/

/**
 * @name User Interface Core
 * @version 0.1 (Alvin)
 * @abstract
 * Selects and includes a UI Toolkit to render widgets.
 * Provides localization support, and many other support
 * routines.
 *
 * @author Sean Micklethwaite
 */

global $gLocale;
global $gStringTable;
global $gUi;

abstract class cUiPage extends cDycmsClientNode implements IDycmsClient
{
	protected $mContentNode;
	protected $mzModuleNode = array();
	protected $mzScripts = array();
	protected $mzStyles = array();
	protected $mzStartScripts = array();
	protected $mzMetaData = array();
	protected $mzDropZone = array();
	
	protected static $mInst;
	
	function __construct(){
		parent::__construct();
		cUiPage::$mInst =& $this;
		
		$this->AddMetaData('ajaxOverride', dyPostOrGet('$$ui-ajaxOverride'));
	}
	
	static function &GetInst(){
		if(!isset(cUiPage::$mInst)){
			cUiPage::$mInst = new cUiPage();
		};
		
		return cUiPage::$mInst;
	}
	
	function &ContentNode()
	{
		if ($this->mContentNode === NULL){
            $this->mContentNode = new cDefaultContentNode();
            $this->AddChild($this->mContentNode);
        }

		return $this->mContentNode;
	}

	function AddModule(cDycmsClientNode &$r_obj)
	{
		if (count($this->mzModuleNode) > 0)
			$this->mzModuleNode[0]->AddChild($r_obj);
	}
	
	function AddScript($sId){
		$this->mzScripts[$sId]
			= PUB.'js/'.$sId.'.lib.js';
		return true;
	}
	
	function AddStartScript($sId, $s){
	 	$this->mzStartScripts[$sId] = $s;
	}
	
	function AddStyle($sId){
		$this->mzStyles[$sId]
			= PUB.'style/'.STYLE_PREFIX.$sId.'.css';
		return true;
	}
	
	function AddMetaData($sId, $sValue){
		$this->mzMetaData[$sId] = $sValue;
	}
	
	function AddExternalScript($sUri){
		$this->mzScripts[] = $sUri;
	}

	function AddModuleContainer(cDycmsClientNode &$r_obj)
	{
		array_push($this->mzModuleNode, $r_obj);
	}

	function SetContentNode(cDycmsClientNode &$r_node)
	{
		$this->mContentNode = $r_node;
	}
	
	function SetDropZone($name, $node) {
		$this->mzDropZone[$name] = $node;
	}
	function GetDropZone($name) {
		return $this->mzDropZone[$name];
	}
}

class cUiContentContainer extends cUiPage
{
	function __construct() {
		$n = new cDycmsClientNode();
		$this->AddChild($n);
		$this->SetContentNode($n);
	}
}



function uiInit()
{
	global $gLocale,$gStringTable;

	$gLocale = "en";
	include_once("$gLocale/string.php");
	$gStringTable = getStringTable();


	//
	// Choose and validate our UI model
	//

	$_SESSION['$$ui'] = $ui = uiDetect();
	switch($ui){
		case 'html':
		case 'xml':
		break;

		default:
		uiErrorCode(EINVALIDARGS, 'Invalid UI model - '.$ui);
		die();
	}

	//
	// Load the base UI widgets library
	//
	require_once(ROOT."$ui/base.php");
}

function uiDetect()
{
    global $gUi;
	return isset($gUi) ? $gUi :
		(isset($_POST['$$ui']) ? $_POST['$$ui'] :
		(isset($_GET['$$ui']) ? $_GET['$$ui']
		:'html'));
}

function uiElAttributes($argv){
	$b = '';
	if(is_array($argv)) foreach($argv as $k=>$v){
		$b .= " $k=\"$v\"";
	}
	return $b;
}

function uiString($name, $cap)
{
	global $gStringTable;
	if($gStringTable[$name])
		return $cap ? ucfirst($gStringTable[$name]) : $gStringTable[$name];
	else
		return $gStringTable['strntfound'] ? $gStringTable['strntfound'].
			": $name" : "MISSING TEXT: $name";
}

function uiMessage($name, $msg = NULL)
{
	global $gLocale;
	if(!ctype_alnum($name))
		return;

	echo '<div class="ui-message">';
	
	$p = $gLocale."/msg-".$name.".php";

	if(($fp = @fopen($p, 'r', 1)) and fclose($fp))
	{
		include($p);
	}
	else
	{
		include($gLocale."/sys-ui-msgnotfound.php");
		echo("uiMessage: $name, Locale: $gLocale");
	};

	if($msg)
		echo("<p><b>".uiString('additional-info',true)."</b><br/>\n$msg</p>");
	echo '</div>';
}

function uiLoadLibrary($lib){
	require_once(ROOT."lib/$lib.lib.php");
}

function uiLoadModule($name, $lib, $id = null, $arg = null) {
	uiLoadLibrary($lib);
	return new ${name}($id, $arg);
}

function uiGetModuleId($name)
{
	$qid = new cDycmsObjectQuery('Module');
	$qid->AddArray(array('classname' => $name));
	if($qid->Execute() != 1)
		throw new dyException("Cannot find module ID");
	return $qid->GetId();
}


function uiTemplate($name) /** Deprecated **/
{
	global $gLocale;
	if(!ctype_alnum($name))
		return;

	$p = "tpl-".$name.".php";

	if(($fp = @fopen($p, 'r', 1)) and fclose($fp))
	{
		include($p);
	}
	else
	{
		include($gLocale."/sys-ui-msgnotfound.php");
		echo("uiTemplate: $name, Locale: $gLocale");
	};
}

function uiIcon($name, $large = false)
{
	if(!ctype_alnum(str_replace('_', '', $name)))
		return;
	$p = PUB."img/icon/$name".($large ? "-large.png" : ".png");

	//if(file_exists($p))
	//{
		echo "<img src='$p' alt='$name' class='icon fixPNG' />";
	//};
};

function uiGetIcon($name, $large = false)
{
	if(!ctype_alnum(str_replace('_', '', $name)))
		return;
	$p = PUB."img/icon/$name".($large ? "-large.png" : ".png");

	//if(file_exists($p))
	//{
		return "<img src='$p' alt='$name' class='icon fixPNG' />";
	//};
};

function uiTryError($e, $msg = NULL)
{
	switch($e)
	{
		case EINVALIDARGS:
		uiMessage("Validation", $msg);
		break;
		case ENOMEM:
		uiMessage("500", ($msg?$msg:"Out of memory"));
		dyLogDump();
		break;
		case ENOTIMPLEMENTED:
		uiMessage("UnderConstruction", $msg);
		break;
		case EINVALIDOP:
		uiMessage("InvalidOp", $msg);
		break;
		case EDB:
		uiMessage("DatabaseError", $msg);
		dyLogDump();
		break;
		case EAUTH:
		uiMessage("SecurityUnauthorized", $msg);
		break;
		case ENOTFOUND:
		uiMessage("404", $msg);
		break;
		case EDUP:
		uiMessage("DuplicateObject", $msg);
		break;
		case EDENIED:
		uiMessage("AccessDenied", $msg);
		break;
		default:
		return false;
	};

	echo "<a href=\"javascript:history.go(-1)\">Back</a>";
	return true;
}

function uiErrorCode($e, $msg = NULL)
{
	if(!uiTryError($e, $msg))
	{
		uiMessage("UnknownError", $msg);
		dyLogDump();
	};
}

?>
